<?php

/**
 *管理员类别
 *
*/
namespace app\admin\controller;

use app\admin\model\AdminRole as RoleModel;
use think\Request;

class AdminRole extends Base
{
    protected $model;
    protected $request;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->request = $request;

        $this->model = new RoleModel();
    }

    public function index()
    {
        $data = $this->model->field('id, name, permission, note')->select();

        $this->assign("data", $data);

        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPOST()) {
            $param = $this->request->param();
            // var_dump($param);
            // exit;

            if (($param['name']) == false) {
                $this->error("角色名不能为空");
            }

            $name = trim($param['name']);

            if ($this->model->where('name', $name)->find()) {
                $this->error("该角色已存在");
            }

            $powers = $param['check'];

            $permission = implode(',', $powers);
         
            try {
                $this->model->save([
                    'name' => $name,
                    'permission' => $permission,
                    'note' => $param['note'],
                ]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success('添加成功', '/admin_role/index');
        } else {
            $powers = model('permission')->where('status', 1)->select();

            $powers = obj_tree($powers);

            $this->assign('powers', $powers);
            return $this->fetch();
        }
    }

    public function edit($id)
    {
        $data = $this->model->find($id);

        if ($this->request->isPOST()) {
            $param = $this->request->param();
            
            if (!$param['name']) {
                $this->error("角色名不能为空");
            }

            $name = trim($param['name']);

            $powers = $param['check'];

            $permission = implode(',', $powers);
         
            try {
                $this->model->save([
                    'name' => $name,
                    'permission' => $permission,
                    'note' => $param['note'],
                ], ['id' => $id]);
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success('编辑成功', '/admin_role/index');
        } else {
            $powers = model('permission')->where('status', 1)->select();

            $powers = obj_tree($powers);

            $role_powers = explode(',', $data->permission);

            $this->assign('powers', $powers);
            $this->assign('role_powers', $role_powers);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    public function del()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $id = $param['id'];

            if (is_array($id)) {
                if (in_array('1', $id)) {
                    return ['code' => 0, 'msg' => '超级管理员无法删除'];
                }
                if (in_array($this->role_id, $id)) {
                    return ['code' => 0, 'msg' => '当前用户角色无法删除'];
                }
            } else {
                if ($id == 1) {
                    return ['code' => 0, 'msg' => '超级管理员无法删除'];
                }
                if ($id == $this->role_id) {
                    return ['code' => 0, 'msg' => '当前用户角色无法删除'];
                }
            }

            if ($this->model->destroy($id)) {
                return ['code'=>2, 'msg'=>'删除成功'];
            } else {
                return ['code'=>1, 'msg'=>'删除失败'];
            }
        }
    }
}
